package com.wejoy.video.middle.station.admin.controller.system;

import com.alibaba.excel.annotation.ExcelProperty;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysBaseParam;
import com.wejoy.video.middle.station.data.domain.system.SysNotice;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.domain.workorder.OrderPhotoLib;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderInfo;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderProcess;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderReview;
import com.wejoy.video.middle.station.data.enums.dict.EnumNoticePopUpsType;
import com.wejoy.video.middle.station.data.enums.dict.EnumNoticeRemindtype;
import com.wejoy.video.middle.station.data.enums.dict.EnumReadNoticeType;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.service.system.ISysBaseParamService;
import com.wejoy.video.middle.station.data.service.system.ISysNoticeService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderInfoService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderProcessService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderReviewService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.workorder.WorkOrderDetailVO;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统消息通知表
 *
 * @author miracle_j
 * @date 2022年11月06日 21:34:52
 */
@RestController
@RequestMapping("/system/sysNotice")
public class SysNoticeController extends BaseController {

    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private IWorkOrderInfoService workOrderInfoService;
    @Autowired
    private ISysBaseParamService sysBaseParamService;
    @Autowired
    private IWorkOrderProcessService workOrderProcessService;
    @Autowired
    private IWorkOrderReviewService workOrderReviewService;

    @Value("${qiniu.prefix}")
    private String imagePrefix;

    /**
     * 保存或更新
     *
     * @param sysNoticeReq sysNoticeReq
     * @return RespJSON
     */
    @PostMapping("/save")
    public RespJSON save(@RequestBody SysNotice sysNoticeReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysNoticeService.save(sysNoticeReq, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysNoticeReq sysNoticeReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys:notice:list")
    public RespJSON getPageList(@RequestBody SysNotice sysNoticeReq,@SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        sysNoticeReq.setToId(loginSysUser.getId());
        int count = sysNoticeService.getCount(sysNoticeReq);
        List<SysNotice> list = new ArrayList<>();
        if (count > 0) {
            list = sysNoticeService.getPageList(sysNoticeReq);
        }
        PageResult result = new PageResult(sysNoticeReq.getPageNo(), sysNoticeReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 查看未读消息
     * @return
     */
    @GetMapping("/getUnreadCount")
    public RespJSON getUnreadCount(@SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser){
        SysNotice where = new SysNotice();
        where.setIsRead(EnumReadNoticeType.HAVE_READ.getCode());
        where.setToId(loginSysUser.getId());
        return RespJSON.success(sysNoticeService.getCount(where));
    }

    /**
     * 获取前端未弹窗提醒的未读消息进行弹窗提醒 因为是前端定时获取，
     * @return
     */
    @GetMapping("/getUnreadRemind")
    public RespJSON getUnreadRemind(@SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser){
        SysNotice where = new SysNotice();
        where.setIsRead(EnumReadNoticeType.HAVE_READ.getCode());
        where.setIsRemind(EnumNoticeRemindtype.NOT_REMINDED.getCode());
        where.setRemindType(EnumNoticePopUpsType.WARN_POPUPS.getCode());
        where.setToId(loginSysUser.getId());
        List<SysNotice> list = sysNoticeService.getList(where);
        list.stream().forEach(item ->{
            item.setIsRemind(EnumNoticeRemindtype.ALERTED.getCode());
            sysNoticeService.save(item,item.getOperator());
        });
        return RespJSON.success(list);
    }
    /**
     * 获取前端未弹窗提醒的警告消息进行弹窗提醒 因为是前端定时获取，所以取消分页
     * @return
     */
    @GetMapping("/getUnreadWarningRemind")
    public RespJSON getUnreadWarningRemind(@SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser){
        SysNotice where = new SysNotice();
        where.setIsRead(EnumReadNoticeType.HAVE_READ.getCode());
        where.setIsRemind(EnumNoticeRemindtype.NOT_REMINDED.getCode());
        where.setRemindType(EnumNoticePopUpsType.WARNING_POPUPS.getCode());
        where.setToId(loginSysUser.getId());
        where.setPageSize(1);
        where.setPageNo(1);
        List<SysNotice> list = sysNoticeService.getPageList(where);
        if(CollectionUtils.isEmpty(list)){
            return RespJSON.success();
        }
        SysNotice sysNotice = list.get(0);
        WorkOrderDetailVO vo=new WorkOrderDetailVO();
        WorkOrderInfo workOrderInfo = workOrderInfoService.getOneById(sysNotice.getOrderId());
        WorkOrderProcess processWhere = new WorkOrderProcess();
        processWhere.setOrderId(workOrderInfo.getId());
        vo.setProcessList(workOrderProcessService.getList(processWhere));
        WorkOrderReview reviewWhere = new WorkOrderReview();
        reviewWhere.setOrderId(workOrderInfo.getId());
        vo.setReviewList(workOrderReviewService.getList(reviewWhere));
        workOrderInfo.setImageUrl(workOrderInfo.getImageUrl());
        vo.setWorkOrderInfo(workOrderInfo);
        vo.setNoticeType(sysNotice.getType());
        SysBaseParam sysBaseParam = sysBaseParamService.getOneByUniCode("SYSTEM_POPUP_TIME");
        vo.setPopupTime(sysBaseParam==null?15L:Long.valueOf(sysBaseParam.getValue()));
        vo.setImageLocation(workOrderInfo.getImageAttributeJson());
        sysNotice.setIsRemind(EnumNoticeRemindtype.ALERTED.getCode());
        sysNoticeService.save(sysNotice,sysNotice.getOperator());
        return RespJSON.success(vo);
    }

    /**
     * 一键已读
     * @return
     */
    @PostMapping("/oneClickRead")
    public RespJSON oneClickRead(@SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser){
        sysNoticeService.oneClickRead(loginSysUser.getId());
        return RespJSON.success();
    }

    /**
     * 单个已读
     * @return
     */
    @PostMapping("/haveRead")
    public RespJSON haveRead(Long id){
        WJAssert.notNull(id, "ID 不合法");
        SysNotice sysNotice = sysNoticeService.getOneById(id);
        if(ObjectUtils.isEmpty(sysNotice)){
            throw new BusinessException("参数错误");
        }
        sysNotice.setIsRead(EnumReadNoticeType.UNREAD.getCode());
        sysNoticeService.save(sysNotice,sysNotice.getOperator());
        return RespJSON.success();
    }


    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(sysNoticeService.getOneById(id));
    }

    /**
    * 审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/audit")
    @ResponseBody
        public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysNoticeService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
    * 取消审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/unaudit")
    @ResponseBody
        public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysNoticeService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }
    /**
     * 删除
     *
     * @param id id
     * @return RespJSON
     */
    @PostMapping("/remove")
    @ResponseBody
    public RespJSON remove(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysNoticeService.delete(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

}
